//
//  NovelDataBase.m
//  UI数据库
//
//  Created by 王建男 on 14-9-9.
//  Copyright (c) 2014年 WJN_work@163.com. All rights reserved.
//

#import "NovelDataBase.h"
#import "NovelModel.h"
#import "Db.h"

@implementation NovelDataBase

+ (void)insertWithModel:(NovelModel *)model
{
    /********************
     *1.获得数据库指针对象
     *2.书写数据库语句
     *3.执行数据库语句
     *********************/
    
    sqlite3 *db = [Db openDataBase];
    NSString *sqlStr = [NSString stringWithFormat:@"insert into novel(book_id,book_name,book_author,book_type) values(%d,'%@','%@','%@')",model.book_id, model.book_name, model.book_author, model.book_type];
    int result = sqlite3_exec(db, [sqlStr UTF8String], nil, nil, nil);
    if (result ==  SQLITE_OK) {
        NSLog(@"添加成功");
    } else {
        NSLog(@"添加失败");
    }
    
    //[Db close];
}

+ (void)deleteWithModel:(NSInteger )model
{
    sqlite3 *db = [Db openDataBase];
    NSString *sqlStr = [NSString stringWithFormat:@"delete from novel where book_id=%d", model];
    int result = sqlite3_exec(db, [sqlStr UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"删除成功");
    } else {
        NSLog(@"删除失败");
    }
    //[Db close];
}

+ (void)updateWithModel:(NovelModel *)model
{
    sqlite3 *db = [Db openDataBase];
    NSString *sqlStr = [NSString stringWithFormat:@"update novel set book_name='%@',book_author='%@',book_type='%@' where book_id=%d", model.book_name, model.book_author, model.book_type, model.book_id];
    int result = sqlite3_exec(db, [sqlStr UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"更新成功");
    } else {
        NSLog(@"更新失败");
    }
}

+ (NSArray *)selectAllNovel
{
    sqlite3 *db = [Db openDataBase];
    //制作sqlite替身
    sqlite3_stmt *stmt = nil;
    //数据库语句
    NSString *sqlStr = [NSString stringWithFormat:@"select * from novel"];
    
    //根据数据库语句将从db中查询到的内容赋值给替身
    int result = sqlite3_prepare(db, [sqlStr UTF8String], -1, &stmt, nil);
    
    //定义数组，用来存储所有数据信息
    NSMutableArray *novel = [[NSMutableArray alloc] init];
    if (result == SQLITE_OK) {
        //根据stmt中的内容，一条一条的获取里面的数据
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            
            //把每条数据的每一个列取出来
            int booId = sqlite3_column_int(stmt, 0);
            const unsigned char * bookName = sqlite3_column_text(stmt, 1);
            const unsigned char * bookAuthor = sqlite3_column_text(stmt, 2);
            const unsigned char * bookType = sqlite3_column_text(stmt, 3);
            
            //将const unsigned char类型转换为NSString类型
            //存入model中
            NovelModel *model = [NovelModel novelModelWithId:booId name:[NSString stringWithUTF8String:(const char *)bookName] author:[NSString stringWithUTF8String:(const char *)bookAuthor] type:[NSString stringWithUTF8String:(const char *)bookType]];
            [novel addObject:model];
        }
    }
    //清除替身
    sqlite3_finalize(stmt);
    return novel;
    
}

+ (NovelModel *)selectId:(NSInteger )bookId
{
    sqlite3 *db = [Db openDataBase];
    //制作sqlite替身
    sqlite3_stmt *stmt = nil;
    //数据库语句
    NSString *sqlStr = [NSString stringWithFormat:@"select * from novel where book_id=%d",bookId];
    
    //根据数据库语句将从db中查询到的内容赋值给替身
    int result = sqlite3_prepare(db, [sqlStr UTF8String], -1, &stmt, nil);
    
    if (result == SQLITE_OK) {
        //根据stmt中的内容，一条一条的获取里面的数据
        if(sqlite3_step(stmt) == SQLITE_ROW) {
            
            //把每条数据的每一个列取出来
            int booId = sqlite3_column_int(stmt, 0);
            const unsigned char * bookName = sqlite3_column_text(stmt, 1);
            const unsigned char * bookAuthor = sqlite3_column_text(stmt, 2);
            const unsigned char * bookType = sqlite3_column_text(stmt, 3);
            
            //将const unsigned char类型转换为NSString类型
            //存入model中
            NovelModel *model = [NovelModel novelModelWithId:booId name:[NSString stringWithUTF8String:(const char *)bookName] author:[NSString stringWithUTF8String:(const char *)bookAuthor] type:[NSString stringWithUTF8String:(const char *)bookType]];\
            //清除替身
            sqlite3_finalize(stmt);
            return model;
        }
    }
   
    return nil;
    
}

@end
